package com.base.service.impl;

import com.base.mapper.voipmapper.SbdDetailMapper;
import com.base.po.param.NetState;
import com.base.po.voip.SbdDetail;
import com.base.po.voip.SbdDetailExample;
import com.base.po.voip.Subscribers;
import com.base.service.SbdDetailService;
import com.base.service.SubscribersService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("SbdDetailService")
public class SbdDetailServiceImpl implements SbdDetailService {
    private final SbdDetailMapper sbdDetailMapper;
    private final SubscribersService subscribersService;

    public SbdDetailServiceImpl(SbdDetailMapper sbdDetailMapper, SubscribersService subscribersService) {
        this.sbdDetailMapper = sbdDetailMapper;
        this.subscribersService = subscribersService;
    }

    @Override
    public int countByExample(SbdDetailExample example) {
        return sbdDetailMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(SbdDetailExample example) {
        return sbdDetailMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return sbdDetailMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SbdDetail record) {
        return sbdDetailMapper.insert(record);
    }

    @Override
    public int insertSelective(SbdDetail record) {
        record.setId(null);
        return sbdDetailMapper.insertSelective(record);
    }

    @Override
    public List<SbdDetail> selectByExample(SbdDetailExample example) {
        return sbdDetailMapper.selectByExample(example);
    }

    @Override
    public SbdDetail selectByPrimaryKey(Integer id) {
        return sbdDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(SbdDetail record, SbdDetailExample example) {
        return sbdDetailMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(SbdDetail record, SbdDetailExample example) {
        return sbdDetailMapper.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKeySelective(SbdDetail record) {
        return sbdDetailMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SbdDetail record) {
        return sbdDetailMapper.updateByPrimaryKey(record);
    }


    @Override
    public BigDecimal getSbdDetailMoney(String userId, Integer statement) throws RuntimeException {
        if (statement.equals(100)) statement = null;
        BigDecimal sbdAdd = sbdDetailMapper.countSbdByUser(userId, statement, 1);
        BigDecimal sbdDel = sbdDetailMapper.countSbdByUser(userId, statement, 0);
        if (sbdAdd.compareTo(sbdDel) < 0) {
            log.warn("用户={},状态={}该用户的金额出现负数 ", userId, statement);
            throw new RuntimeException("该用户的金额出现负数");
        }
        return sbdAdd.subtract(sbdDel);
    }

    @Override
    @Transactional
    public NetState delSbdDetailMoney(String sbdUser, String sbdTradeTo, String sbdTradeContext, BigDecimal sbdTradeMoney, Integer sbdStatus, Integer sbdStatement) {
        log.warn("[delSbdDetailMoney] 接受参数：sbdUser={},sbdTradeTo={},sbdTradeContext={},sbdTradeMoney={},sbdStatus={},sbdStatement={}", sbdUser, sbdTradeTo, sbdTradeContext, sbdTradeMoney, sbdStatus, sbdStatement);
        Subscribers sub = subscribersService.selectByPrimaryKey(sbdUser);
        if (sub == null) return new NetState(NetState.FEAILED, "用户不存在");

        if (StringUtils.isEmpty(sbdTradeContext)) return new NetState(NetState.FEAILED, "sbdTradeContext必填项");
        if (sbdTradeMoney.compareTo(new BigDecimal(0)) < 0) return new NetState(NetState.FEAILED, "扣费金额必须是正数");
        if (!sbdStatus.equals(1) && !sbdStatus.equals(2)) return new NetState(NetState.FEAILED, "状态只能是1或2");

//        if (sbdStatus.equals(1)) {
//            BigDecimal allMoney = sbdDetailStockService.getUserStock(sbdUser, 100);
//            if (allMoney.compareTo(sbdTradeMoney) < 0) {
//                SbdDetail sbdDetail = new SbdDetail();
//                sbdDetail.setSbdStatement(sbdStatement);
//                sbdDetail.setSbdStatus(sbdStatus);
//                sbdDetail.setSbdTime(new Date());
//                sbdDetail.setSbdTradeMoney(allMoney);
//                sbdDetail.setSbdUser(sbdUser);
//                sbdDetail.setSbdTradeContext("该用户的锁仓金额已满,请充值提升锁仓金额");
//                sbdDetail.setSbdTradeTo(sbdTradeTo);
//                sbdDetailMapper.insertSelective(sbdDetail);
//                SbdDetailStock sbdDetailStock=new SbdDetailStock(null,sbdUser,sbdTradeMoney,0,1,new Date());
//                sbdDetailStockService.insertSelective(sbdDetailStock);
//                return new NetState(NetState.SUCCESS_CONN, "成功返回部分");
//            }
//        }
        SbdDetail sbdDetail = new SbdDetail();
        sbdDetail.setSbdStatement(sbdStatement);
        sbdDetail.setSbdStatus(sbdStatus);
        sbdDetail.setSbdTime(new Date());
        sbdDetail.setSbdTradeMoney(sbdTradeMoney);
        sbdDetail.setSbdUser(sbdUser);
        sbdDetail.setSbdTradeTo(sbdTradeTo);
        sbdDetail.setSbdTradeContext(sbdTradeContext);
        sbdDetailMapper.insertSelective(sbdDetail);
        return new NetState(NetState.SUCCESS_CONN, "成功");
    }

    @Override
    public List<SbdDetail> getDetailMoneyList(String userId, Integer statement, Integer pageNo) {
        if (statement.equals(100)) statement = null;
        Page<SbdDetail> page = PageHelper.startPage(pageNo, 10).setOrderBy("sbd_time desc");
        List<SbdDetail> data = sbdDetailMapper.selectListWeb(userId, statement);
        return page.getResult();
    }


    @Override
    public NetState transferMoney(String userId, String getMoneyUserId, BigDecimal money) {
        Subscribers sub=subscribersService.selectByPrimaryKey(userId);
        Subscribers getSubMoney=subscribersService.selectByPrimaryKey(getMoneyUserId);
        if (sub == null || getSubMoney == null) {
            return new NetState(NetState.PARAMS_ERROR, "传入的用户不存在");
        }
        BigDecimal sbdAdd = sbdDetailMapper.countSbdByUser(userId, null, 1);
        BigDecimal sbdDel = sbdDetailMapper.countSbdByUser(userId, null, 0);
        if (sbdAdd.subtract(sbdDel).subtract(money).compareTo(new BigDecimal(0)) < 0) {
            return new NetState(NetState.FEAILED, "金额不足,无法转账");
        }
        SbdDetail userDetail = new SbdDetail(null, userId, getMoneyUserId, "向"+getSubMoney.getMobile()+"转账减少", money, 0, 3, new Date());
        SbdDetail getMoneyDetail = new SbdDetail(null, getMoneyUserId, userId, sub.getMobile()+"向你转账增多", money, 1, 3, new Date());
        sbdDetailMapper.insertSelective(userDetail);
        sbdDetailMapper.insertSelective(getMoneyDetail);
        return new NetState(NetState.SUCCESS_CONN, "转账SBD成功");
    }

    @Override
    public Integer getDetailMoneyCount(String subId, Integer statement) {
        if (statement.equals(100)) statement = null;
        Integer data = sbdDetailMapper.countListWeb(subId, statement);
        return data;
    }
}
